package com.tsing.bms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tsing.bms.entity.Building;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
public interface BuildingMapper extends BaseMapper<Building> {

    @Select("<script>" +
            "select 1 from t_building b\n" +
            "inner join t_unit u on b.id=u.building_id\n" +
            "where b.id in\n" +
            "<foreach collection='ids' item='item' open='(' close=')' separator=',' >\n" +
            "#{item}\n" +
            "</foreach>\n" +
            "limit 1" +
            "</script>")
    String haveRelevance(@Param("ids") List<String> ids);

    @Select("select count(id)\n" +
            "from t_unit \n" +
            "where building_id=#{id}")
    long findUnitCount(@Param("id") String id);

    @Select("select count(h.id)\n" +
            "from t_unit u\n" +
            "inner join t_house h on u.id=h.unit_id\n" +
            "where u.building_id=#{id}")
    long findHouseCount(@Param("id") String id);

    @Select("select count(u.id)\n" +
            "from t_user u \n" +
            "inner join t_house h on u.house_id=h.id\n" +
            "inner join t_unit un on h.unit_id=un.id\n" +
            "inner join t_building b on un.building_id=b.id\n" +
            "where b.id=#{id}")
    long findCqyzCount(@Param("id") String id);

    @Select("select count(h.id)\n" +
            "from t_house h\n" +
            "inner join t_unit un on h.unit_id=un.id\n" +
            "inner join t_building b on un.building_id=b.id\n" +
            "where b.id=#{id} and h.status=3")
    long findRzyzCount(@Param("id") String id);

    @Select("select count(h.id)\n" +
            "from t_house h\n" +
            "inner join t_unit un on h.unit_id=un.id\n" +
            "inner join t_building b on un.building_id=b.id\n" +
            "where b.id=#{id} and h.status=1")
    long findRzzhCount(@Param("id") String id);

}
